using System;
using System.Data;
using System.Data.SqlTypes;
using System.Data.SqlClient;

namespace Model
{
    public partial class Tbl003_News : DBInteractionBase
	{
		#region Class Member Declarations
			private SqlInt64		_nTotalViews;
			private SqlBoolean		_nStatus;
			private SqlDateTime		_nCreateDate, _nPostDate, _nLastDateModify;
			private SqlInt32		_grpID;
			private SqlString		_caID, _caIDOld, _nTile, _nID, _nUserName, _ncontent, _nBrief, _nImages;
		#endregion


		public Tbl003_News()
		{
			// Nothing for now.
		}


		public override bool Insert()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_Insert]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _nID));
				cmdToExecute.Parameters.Add(new SqlParameter("@NTile", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nTile));
				cmdToExecute.Parameters.Add(new SqlParameter("@NBrief", SqlDbType.NVarChar, 500, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nBrief));
				int	length = 0;
				if(!_ncontent.IsNull)
				{
					length = _ncontent.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@Ncontent", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _ncontent));
				cmdToExecute.Parameters.Add(new SqlParameter("@NCreateDate", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nCreateDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@NPostDate", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nPostDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@NLastDateModify", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nLastDateModify));
				cmdToExecute.Parameters.Add(new SqlParameter("@NTotalViews", SqlDbType.BigInt, 8, ParameterDirection.Input, true, 19, 0, "", DataRowVersion.Proposed, _nTotalViews));
				cmdToExecute.Parameters.Add(new SqlParameter("@NImages", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nImages));
				cmdToExecute.Parameters.Add(new SqlParameter("@NUserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nUserName));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@NStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nStatus));
				cmdToExecute.Parameters.Add(new SqlParameter("@GrpID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _grpID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::Insert::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Update()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_Update]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _nID));
				cmdToExecute.Parameters.Add(new SqlParameter("@NTile", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nTile));
				cmdToExecute.Parameters.Add(new SqlParameter("@NBrief", SqlDbType.NVarChar, 500, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nBrief));
				int	length = 0;
				if(!_ncontent.IsNull)
				{
					length = _ncontent.Value.Length;
				}
				cmdToExecute.Parameters.Add(new SqlParameter("@Ncontent", SqlDbType.NText, length, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _ncontent));
				cmdToExecute.Parameters.Add(new SqlParameter("@NCreateDate", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nCreateDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@NPostDate", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nPostDate));
				cmdToExecute.Parameters.Add(new SqlParameter("@NLastDateModify", SqlDbType.DateTime, 8, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nLastDateModify));
				cmdToExecute.Parameters.Add(new SqlParameter("@NTotalViews", SqlDbType.BigInt, 8, ParameterDirection.Input, true, 19, 0, "", DataRowVersion.Proposed, _nTotalViews));
				cmdToExecute.Parameters.Add(new SqlParameter("@NImages", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nImages));
				cmdToExecute.Parameters.Add(new SqlParameter("@NUserName", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nUserName));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@NStatus", SqlDbType.Bit, 1, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _nStatus));
				cmdToExecute.Parameters.Add(new SqlParameter("@GrpID", SqlDbType.Int, 4, ParameterDirection.Input, true, 10, 0, "", DataRowVersion.Proposed, _grpID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::Update::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public bool UpdateAllWCaIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_UpdateAllWCaIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _caID));
				cmdToExecute.Parameters.Add(new SqlParameter("@CaIDOld", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caIDOld));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::UpdateAllWCaIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override bool Delete()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_Delete]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _nID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				cmdToExecute.ExecuteNonQuery();
				return true;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::Delete::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
			}
		}


		public override DataTable SelectOne()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_SelectOne]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl003_News");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@NID", SqlDbType.NVarChar, 50, ParameterDirection.Input, false, 0, 0, "", DataRowVersion.Proposed, _nID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				if(toReturn.Rows.Count > 0)
				{
					_nID = (string)toReturn.Rows[0]["NID"];
					_nTile = toReturn.Rows[0]["NTile"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["NTile"];
					_nBrief = toReturn.Rows[0]["NBrief"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["NBrief"];
					_ncontent = toReturn.Rows[0]["Ncontent"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["Ncontent"];
					_nCreateDate = toReturn.Rows[0]["NCreateDate"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["NCreateDate"];
					_nPostDate = toReturn.Rows[0]["NPostDate"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["NPostDate"];
					_nLastDateModify = toReturn.Rows[0]["NLastDateModify"] == System.DBNull.Value ? SqlDateTime.Null : (DateTime)toReturn.Rows[0]["NLastDateModify"];
					_nTotalViews = toReturn.Rows[0]["NTotalViews"] == System.DBNull.Value ? SqlInt64.Null : (Int64)toReturn.Rows[0]["NTotalViews"];
					_nImages = toReturn.Rows[0]["NImages"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["NImages"];
					_nUserName = toReturn.Rows[0]["NUserName"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["NUserName"];
					_caID = toReturn.Rows[0]["CaID"] == System.DBNull.Value ? SqlString.Null : (string)toReturn.Rows[0]["CaID"];
					_nStatus = toReturn.Rows[0]["NStatus"] == System.DBNull.Value ? SqlBoolean.Null : (bool)toReturn.Rows[0]["NStatus"];
					_grpID = toReturn.Rows[0]["GrpID"] == System.DBNull.Value ? SqlInt32.Null : (Int32)toReturn.Rows[0]["GrpID"];
				}
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::SelectOne::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public override DataTable SelectAll()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_SelectAll]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl003_News");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::SelectAll::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		public DataTable SelectAllWCaIDLogic()
		{
			SqlCommand	cmdToExecute = new SqlCommand();
			cmdToExecute.CommandText = "dbo.[sp_tbl003_News_SelectAllWCaIDLogic]";
			cmdToExecute.CommandType = CommandType.StoredProcedure;
			DataTable toReturn = new DataTable("tbl003_News");
			SqlDataAdapter adapter = new SqlDataAdapter(cmdToExecute);

			// Use base class' connection object
			cmdToExecute.Connection = _mainConnection;

			try
			{
				cmdToExecute.Parameters.Add(new SqlParameter("@CaID", SqlDbType.NVarChar, 50, ParameterDirection.Input, true, 0, 0, "", DataRowVersion.Proposed, _caID));

				// Open connection.
				_mainConnection.Open();

				// Execute query.
				adapter.Fill(toReturn);
				return toReturn;
			}
			catch(Exception ex)
			{
				// some error occured. Bubble it to caller and encapsulate Exception object
				throw new Exception("Tbl003_News::SelectAllWCaIDLogic::Error occured.", ex);
			}
			finally
			{
				// Close connection.
				_mainConnection.Close();
				cmdToExecute.Dispose();
				adapter.Dispose();
			}
		}


		#region Class Property Declarations
		public SqlString NID
		{
			get
			{
				return _nID;
			}
			set
			{
				SqlString nIDTmp = (SqlString)value;
				if(nIDTmp.IsNull)
				{
					throw new ArgumentOutOfRangeException("NID", "NID can't be NULL");
				}
				_nID = value;
			}
		}


		public SqlString NTile
		{
			get
			{
				return _nTile;
			}
			set
			{
				_nTile = value;
			}
		}


		public SqlString NBrief
		{
			get
			{
				return _nBrief;
			}
			set
			{
				_nBrief = value;
			}
		}


		public SqlString Ncontent
		{
			get
			{
				return _ncontent;
			}
			set
			{
				_ncontent = value;
			}
		}


		public SqlDateTime NCreateDate
		{
			get
			{
				return _nCreateDate;
			}
			set
			{
				_nCreateDate = value;
			}
		}


		public SqlDateTime NPostDate
		{
			get
			{
				return _nPostDate;
			}
			set
			{
				_nPostDate = value;
			}
		}


		public SqlDateTime NLastDateModify
		{
			get
			{
				return _nLastDateModify;
			}
			set
			{
				_nLastDateModify = value;
			}
		}


		public SqlInt64 NTotalViews
		{
			get
			{
				return _nTotalViews;
			}
			set
			{
				_nTotalViews = value;
			}
		}


		public SqlString NImages
		{
			get
			{
				return _nImages;
			}
			set
			{
				_nImages = value;
			}
		}


		public SqlString NUserName
		{
			get
			{
				return _nUserName;
			}
			set
			{
				_nUserName = value;
			}
		}


		public SqlString CaID
		{
			get
			{
				return _caID;
			}
			set
			{
				_caID = value;
			}
		}
		public SqlString CaIDOld
		{
			get
			{
				return _caIDOld;
			}
			set
			{
				_caIDOld = value;
			}
		}


		public SqlBoolean NStatus
		{
			get
			{
				return _nStatus;
			}
			set
			{
				_nStatus = value;
			}
		}


		public SqlInt32 GrpID
		{
			get
			{
				return _grpID;
			}
			set
			{
				_grpID = value;
			}
		}
		#endregion
	}
}
